﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Net;
using System.Runtime.Serialization;
using WechatModule.Tool;
using WechatModule.API;
using WechatModule.Model;
using WanWebApplication.Common;
namespace WxTest.VegetaTest
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod.ToLower() == "post")
            {
                #region Post
                string postStr = "";
                Stream s = System.Web.HttpContext.Current.Request.InputStream;
               byte[] b = new byte[s.Length];
               s.Read(b, 0, (int)s.Length);
               postStr = Encoding.UTF8.GetString(b);

                if (!string.IsNullOrEmpty(postStr))
                {
                    //封装请求类
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(postStr);
                    XmlElement rootElement = doc.DocumentElement;

                    XmlNode MsgType = rootElement.SelectSingleNode("MsgType");

                    WechatRequest requestXML = new WechatRequest();
                    requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText;
                    requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;
                    requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText;
                    requestXML.MsgType = MsgType.InnerText;


                    if (requestXML.MsgType == "text")
                    {
                        requestXML.Content = rootElement.SelectSingleNode("Content").InnerText;
                        requestXML.MsgId = rootElement.SelectSingleNode("MsgId").InnerText;
                    }
                    else if (requestXML.MsgType == "location")
                    {
                        requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText;
                        requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText;
                        requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText;
                        requestXML.Label = rootElement.SelectSingleNode("Label").InnerText;
                    }
                    else if (requestXML.MsgType == "image")
                    {
                        requestXML.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText;
                    }
                    else if (requestXML.MsgType == "event")
                    {
                        requestXML.Wxevent = rootElement.SelectSingleNode("Event").InnerText;
                        requestXML.EventKey = rootElement.SelectSingleNode("EventKey").InnerText;
                    }

                    //Log.WriteTxt("<!--粉丝发送过来" + requestXML.MsgType + "\n" + postStr + "-->\n", this);
                    //回复消息
                    ResponseMsg(requestXML);
                }
                #endregion
            }
            else
            {
                WechatBaseAPI.Valid(this);
            }
        }
        /// <summary>
        /// 回复消息(微信信息返回)
        /// </summary>
        /// <param name="weixinXML"></param>
        private void ResponseMsg(WechatRequest requestXML)
        {
            string resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + WechatModule.Tool.DatetimeTool.ConvertDateTimeInt(DateTime.Now) + "</CreateTime>";
            try
            {
                if (requestXML.MsgType == "text")
                {
                    resxml += "<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[暂不具备回馈功能！]]></Content><FuncFlag>0</FuncFlag></xml>";
                }
                else if (requestXML.MsgType == "location")
                {
                    resxml += "<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[你发过来的是地理消息，我会马上会看哦\n]]></Content><FuncFlag>0</FuncFlag></xml>";
                }
                else if (requestXML.MsgType == "image")
                {
                    resxml += "<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[你发过来的图片，我会马上会看哦\n]]></Content><FuncFlag>0</FuncFlag></xml>";
                }
                else if (requestXML.MsgType == "event")
                {
                    if (requestXML.Wxevent == "unsubscribe")
                    {
                        //取消关注
                       
                    }
                    else if (requestXML.Wxevent == "subscribe")
                    {//订阅


                    }
                    #region Click
                    else if (requestXML.Wxevent.ToUpper() == "CLICK")
                    {
                        if (requestXML.EventKey == "V01_S01")
                        { //平台注册

                            int count = 1;
                            resxml += "<MsgType><![CDATA[news]]></MsgType><ArticleCount>" + count + "</ArticleCount><Articles>";
                            resxml += "<item><Title><![CDATA[系统供应商注册]]></Title><Description><![CDATA[以本微信关联并注册相关信息]]></Description><PicUrl><![CDATA[" + GlobalVar.WechatBtn0PictureUrl + "]]></PicUrl><Url><![CDATA[" + GlobalVar.WechatBtn0Url + "]]></Url></item>";//URL是点击之后跳转去那里，这里跳转到百度
                            resxml += "</Articles><FuncFlag>0</FuncFlag></xml>";
                        }
                        else if (requestXML.EventKey == "V01_S02")
                        { //平台标准说明
                            int count = 1;
                            resxml += "<MsgType><![CDATA[news]]></MsgType><ArticleCount>" + count + "</ArticleCount><Articles>";
                            resxml += "<item><Title><![CDATA[标准说明]]></Title><Description><![CDATA[点击进入供应商二维码标准说明]]></Description><PicUrl><![CDATA[" + GlobalVar.WechatBtn1PictureUrl + "]]></PicUrl><Url><![CDATA[" + GlobalVar.WechatBtn1Url + "]]></Url></item>";//URL是点击之后跳转去那里，这里跳转到百度
                            resxml += "</Articles><FuncFlag>0</FuncFlag></xml>";

                        }
                        else if (requestXML.EventKey == "V01_S03")
                        { //平台信息
                            resxml += "<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[系统版本1.0.0]]></Content><FuncFlag>0</FuncFlag></xml>";
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
               RgUtility.Debug.DebugLog.WriteLine ("异常：" + ex.Message);
            }
            //Log.WriteTxt("<!--返回给粉丝的消息：-->\n" + resxml, this);
            Response.Write(resxml);
            Response.End();
        }






    }
}

